//
//  ViewController.swift
//  BlowUpHeader
//
//  Created by zheng on 16/7/12.
//  Copyright © 2016年 Smiletaoist. All rights reserved.
//

import UIKit


let Screen_Width = UIScreen.mainScreen().bounds.width
let Screen_Height = UIScreen.mainScreen().bounds.height
let ImageHeight: CGFloat = 300.0


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        view.addSubview(tableView)
        tableView.addSubview(erinImageView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRectMake(0, 0, Screen_Width, Screen_Height), style: .Grouped)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "identifier")
        tableView.contentInset = UIEdgeInsetsMake(ImageHeight, 0, 0, 0)
        return tableView
    }()
    
    private lazy var erinImageView: UIImageView = {
        let imageView = UIImageView(frame: CGRectMake(0, -ImageHeight, Screen_Width, ImageHeight))
        imageView.image = UIImage(named: "467.jpg")
        return imageView
    }()
}

extension ViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("identifier") as UITableViewCell!
        cell.textLabel?.text = "indexPath.row = \(indexPath.row)"
        return cell
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.min
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.min
    }
}

extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let contentOfSetY = scrollView.contentOffset.y
        let contentOfSetX = (contentOfSetY + ImageHeight) / 2.0
        if contentOfSetY < -ImageHeight {
            var rect = erinImageView.frame
            rect.origin.y = contentOfSetY
            rect.size.height = -contentOfSetY
            rect.origin.x = contentOfSetX
            rect.size.width = Screen_Width + fabs(contentOfSetX) * 2
            erinImageView.frame = rect
        }
    }
}


